<?php
// +----------------------------------------------------------------------
// | title: 标题注释
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2019/7/30 14:06
// +----------------------------------------------------------------------

namespace app\single\model;


use think\db\Where;
use think\Exception;
use think\exception\PDOException;
use think\Model;

class MailModel extends Model
{
    protected $table = "l_mail";

    /**
     * @param array $map
     * @return mixed
     */
    public function checkInfo(array $map = array())
    {
        return $this->where("status","<>",2)->where(new Where($map))->value("id");
    }

    /**
     * 系统邮件列表处理
     * @param $listArr
     * @return array
     */
    public function getList($listArr): array
    {
        $data = array();
        $itemConf = getDiffConfig("item");
        foreach($listArr as $row){
            if($row["attachment"]){
                $sendItemArr = json_decode($row["attachment"],true);
                $row["send_item_string"] = "";
                foreach($sendItemArr as $val){
                    $row["send_item_string"] .= "[".$val["code"]."] ".$itemConf[$val["code"]]." * ".$val["num"]."<br/>";
                }
            }
            if($row["condition"]){
                $conditionArr = json_decode($row["condition"],true);
                $vo = $conditionArr["info"];
                if($conditionArr["target"] == 1){
                    $row["target"] = "";
//                    $vo["career"] && $row["target"] .= "职业：".implode(",",$vo['career'])."<br/>";
                    ($vo["min_level"] != 0 || $vo["max_level"] != 100) && $row["target"] .= "等级：".$vo['min_level'].'~'.$vo['max_level']."<br/>";
                    ($vo["last_days"] > 0) && $row["target"] .= $vo['last_days']."天内有登陆<br/>";
                    ($vo["is_pay"] > 0) && $row["target"] .= "付费玩家<br/>";
                    ($vo["min_vip"] > 0) && $row["target"] .= "vip等级：".$vo['min_vip'].'~'.$vo['max_vip']."<br/>";
                    if(empty($row["target"])){
                        $row["target"] = "全部玩家";
                    }
                }else{
                    $row["target"] = implode(",",$vo);
                }
            }
            $data[] = $row;
        }
        return $data;
    }

    /**
     * 更新邮件发送情况
     * @param $id
     * @param array $data 更新字段数组
     * @return bool
     * @throws Exception
     * @throws PDOException
     */
    public function updateStatus($id, array $data = array()): bool
    {
        if($id <= 0){
            return false;
        }
        return $this->where("id",$id)->update($data);
    }
}